<template>
  <div class="app-container">
    <div v-if="activePage === 'list'">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="课件名称">
          <el-input
            v-model="queryParams.courseName"
            placeholder="请输入课件名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:post:add']"
            >新增</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:post:export']"
            >导出</el-button
          >
        </el-col> -->
      </el-row>

      <el-table
        v-loading="loading"
        :data="pageList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="课程编号" align="center" prop="id" />
        <el-table-column label="课程名称" align="center" prop="courseName" />
        <el-table-column label="讲师" align="center" prop="postCode" />
        <el-table-column label="创建时间" align="center" prop="gmtCreate" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-setting"
              @click="handleChapter(scope.row)"
              >章节</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageCurrent"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <add-course
      v-if="activePage === 'add'"
      :activeData="activeData"
      @cancel="cancel"
      @refresh="refresh"
    />
    <chapter
      v-if="activePage === 'chapter'"
      :activeData="activeData"
      @cancel="cancel"
    ></chapter>
  </div>
</template>
  
  <script>
import { QueryCoursePage, DeleteCourse } from "@/api/course/course.js";
import AddCourse from "./addCourse.vue";
import Chapter from "./chapter.vue";

export default {
  name: "Post",
  dicts: ["sys_normal_disable"],
  components: { AddCourse, Chapter },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      pageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      activePage: "list",
      // 查询参数
      queryParams: {
        pageCurrent: 1,
        pageSize: 10,
        courseName: "",
      },
      activeData: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" },
        ],
        postCode: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" },
        ],
        postSort: [
          { required: true, message: "岗位顺序不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      QueryCoursePage(this.queryParams)
        .then((res) => {
          if (res.code === 200) {
            this.pageList = res.data.list;
            this.total = res.data.totalCount;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.activePage = "list";
    },
    refresh() {
      this.cancel();
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageCurrent = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.courseName = "";
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.postId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.activeData = {};
      this.activePage = "add";
    },
    handleChapter(row) {
      this.activeData = row;
      this.activePage = "chapter";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.activeData = row;
      this.activePage = "add";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.postId != undefined) {
            updatePost(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.showAdd = false;
              this.getList();
            });
          } else {
            addPost(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.showAdd = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除课程编号为"' + id + '"的数据项？')
        .then(function () {
          return DeleteCourse(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/post/export",
        {
          ...this.queryParams,
        },
        `post_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
  